#include "registerdialog.h"
#include "ui_registerdialog.h"
#include <QMessageBox>
#include "userdao.h"

RegisterDialog::RegisterDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::RegisterDialog)
{
    ui->setupUi(this);
}

RegisterDialog::~RegisterDialog()
{
    delete ui;
}

void RegisterDialog::on_pushButton_register_clicked()
{
    QString name = ui->lineEdit_username->text();
    QString pw = ui->lineEdit_password->text();
    QString repw = ui->lineEdit_password_qr->text();
    if(name.isEmpty() || pw.isEmpty() || repw.isEmpty())
    {
        QMessageBox::warning(this,"警告","输入内容不能为空！");
        return;
    }
    if(pw != repw)
    {
        QMessageBox::warning(this,"警告","两次密码不相同，请检查！");
        return;
    }

    UserEntity ue;
    ue.name = name;
    ue.password = pw;
    bool ok;
    UserDao::getInstance()->insertUser(ue,ok);
    if(!ok)
    {
        QMessageBox::warning(this,"警告","注册失败！");
        return;
    }
    else
    {
        QMessageBox::information(this,"提示","注册成功，可以使用循声音乐播放器了！");
        close();
    }
}

void RegisterDialog::on_pushButton_exit1_clicked()
{
    close();
}
